﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;


namespace SysObj
{
    public class Pc_DinhMuc : Lib.Bases.BaseClass
    {
        Pc_DinhMuc_NguyenLieu _ctiet = new Pc_DinhMuc_NguyenLieu();

        #region Prop
        public System.Int32 IdDinhMuc
        {
            get;
            set;
        }

        public System.DateTime Ngay
        {
            get;
            set;
        }

        public System.Int32 IdThucDon
        {
            get;
            set;
        }

        public System.String DienGiai
        {
            get;
            set;
        }

        #endregion

        public DataTable D_HH { get; set; }

        #region  Constructors
        public Pc_DinhMuc()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        #endregion

        #region Methods
        public override void ResetData()
        {
            IdDinhMuc = 0;
            Ngay = DateTime.Now;
            IdThucDon = 0;
            DienGiai = string.Empty;
        }

        public void ReadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_Pc_DinhMuc", " Command,Ngay,IdThucDon", "ReadData", Cvert.ToShortDate(Ngay), IdThucDon);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void ReadDataByID(int v_IdDinhMuc)
        {
            DataTable dt = new DataTable();
            dt = m_DataLayer.ExecDataTable("SP_Pc_DinhMuc", " Command,IdDinhMuc", "ReadDataByID", v_IdDinhMuc);
            this.ResetData();
            if (dt == null || dt.Rows.Count <= 0)
                return;
            this.FillData(dt.Rows[0]);
        }

        public void GetNewestByMenuID(int v_IdHangHoa)
        {
            DataTable dt = new DataTable();
            dt = m_DataLayer.ExecDataTable("SP_Pc_DinhMuc", " Command,IdThucDon", "GetNewestByMenuID", v_IdHangHoa);
            this.ResetData();
            if (dt == null || dt.Rows.Count <= 0)
                return;
            this.FillData(dt.Rows[0]);
        }

        public override DataTable LoadData()
        {
            return null;
        }

        public DataTable GetHistoryByMenuID(int v_IdHangHoa)
        {
            return m_DataLayer.ExecDataTable("SP_Pc_DinhMuc", " Command,IdThucDon", "GetHistoryByMenuID", v_IdHangHoa);
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdDinhMuc"))
                IdDinhMuc = Lib.Data.ValueType.GetValue(row["IdDinhMuc"], IdDinhMuc);
            if (row.Table.Columns.Contains("Ngay"))
                Ngay = Lib.Data.ValueType.GetValue(row["Ngay"], Ngay);
            if (row.Table.Columns.Contains("IdThucDon"))
                IdThucDon = Lib.Data.ValueType.GetValue(row["IdThucDon"], IdThucDon);
            if (row.Table.Columns.Contains("DienGiai"))
                DienGiai = Lib.Data.ValueType.GetValue(row["DienGiai"], DienGiai);
        }

        public DataTable GetDetail()
        {
            return _ctiet.GetDetail(m_DataLayer, IdDinhMuc);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdDinhMuc", IdDinhMuc);
                Lib.Data.ParamHolder.AddParam("Ngay", Cvert.ToShortDate(Ngay));
                Lib.Data.ParamHolder.AddParam("IdThucDon", IdThucDon);
                Lib.Data.ParamHolder.AddParam("DienGiai", DienGiai);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_PC_DINHMUC", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdDinhMuc = Cvert.ToInt32(output["IdDinhMuc"], 0);

                _ctiet.OnDelete(m_DataLayer, IdDinhMuc);
                _ctiet.Insert(m_DataLayer,D_HH, IdDinhMuc);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                _ctiet.OnDelete(m_DataLayer, IdDinhMuc);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("Ngay", Cvert.ToShortDate(Ngay));
                Lib.Data.ParamHolder.AddParam("IdThucDon", IdThucDon);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_PC_DINHMUC", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }


        #endregion
    }
}
